import sys
import csv

RED = (0, set())
GREEN = (120, set())
BLUE = (240, set())
ORANGE = (40, set())
PURPLE = (300, set())
CYAN = (180, set())

bounds = {'RLedOn.csv' : RED, 'GLedOn.csv' : GREEN, 'BLedOn.csv' : BLUE, 'OLedOn.csv' : ORANGE, 'PLedOn.csv' : PURPLE, 'CLedOn.csv' : CYAN}

alpha = -1

def calculateMean(sum, count):
    return sum / float(count)
        
def calculateHue(red, green, blue):
    r = red / 255.0
    g = green / 255.0
    b = blue / 255.0

    max_value = max(r, g, b)
    min_value = min(r, g, b)
  
    if (max_value == r):
        if (g >= b):
            return 60 * (g-b)/(max_value - min_value)
        else:
            return 60 * (g-b)/(max_value - min_value) + 360
    elif (max_value == g):
        return 60 * (b-r)/(max_value - min_value) + 120
    else:
        return 60 * (r-g)/(max_value - min_value) + 240

best_points = set()

while(len(best_points) == 0):
    alpha += 1
    
    for file_name in bounds.keys():        
        file = open(file_name)
        reader = csv.DictReader(file)

        upper_bound = (bounds[file_name][0] + alpha) % 360
        lower_bound = bounds[file_name][0] - alpha
        if (lower_bound < 0):
            lower_bound += 360

        point_set = bounds[file_name][1]
        point_set.clear()
        
        is_red = (file_name[0] == 'R')

        red_sum = 0
        green_sum = 0
        blue_sum = 0

        count = 0
        result = []
        index = 0
        
        for row in reader:
            if row['Cap'] == str(index):
                red = int(row['Red'])
                green = int(row['Green'])
                blue = int(row['Blue'])
            
                red_sum += red
                
                green_sum += green
                
                blue_sum += blue
                
                count += 1
            else:
                result.insert(index, {})
            
                result[index]['Red'] = calculateMean(red_sum, count)
                result[index]['Green'] = calculateMean(green_sum, count)
                result[index]['Blue'] = calculateMean(blue_sum, count)
            
                red = int(row['Red'])
                green = int(row['Green'])
                blue = int(row['Blue'])
            
                red_sum = red
                
                green_sum = green
                
                blue_sum = blue
                
                count = 1
                index = int(row['Cap'])

        result.insert(index, {})
            
        result[index]['Red'] = calculateMean(red_sum, count)
        result[index]['Green'] = calculateMean(green_sum, count)
        result[index]['Blue'] = calculateMean(blue_sum, count)
                
        for red in xrange(16):
            for green in xrange(16):
                for blue in xrange(16):
                    red_mean = result[red]['Red']
                    green_mean = result[green]['Green']
                    blue_mean = result[blue]['Blue']
                    hue = calculateHue(red_mean, green_mean, blue_mean)
                    point = blue + (green * 16) + (red * 16 * 16)
                    if (is_red):
                        if (lower_bound >= hue or hue >= upper_bound):
                            point_set.add(point)
                    else:
                        if (lower_bound <= hue <= upper_bound):
                            point_set.add(point)
                            
        best_points = RED[1].intersection(GREEN[1], BLUE[1], ORANGE[1], PURPLE[1], CYAN[1])

print "Alpha: ", alpha
print "Red: ", RED[0] - alpha, "~", RED[0] + alpha
print "Green: ", GREEN[0] - alpha, "~", GREEN[0] + alpha
print "Blue: ", BLUE[0] - alpha, "~", BLUE[0] + alpha
print "Orange: ", ORANGE[0] - alpha, "~", ORANGE[0] + alpha
print "Purple: ", PURPLE[0] - alpha, "~", PURPLE[0] + alpha
print "Cyan: ", CYAN[0] - alpha, "~", CYAN[0] + alpha
print "Best points: ", best_points